package com.cssw.fyzb.busi.api.user;

import com.cssw.fyzb.busi.convert.Converter;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.core.exception.BizException;
import com.cssw.fyzb.core.model.dodb.AvatarPendantDef;
import com.cssw.fyzb.core.model.dodb.User;
import com.cssw.fyzb.core.model.vo.user.AvatarPendantDefVo;
import com.cssw.fyzb.core.util.StringUtil;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 更新头像挂件
 *
 * @author YangYiQun
 */
@Function(description = "更新头像挂件", parameters = {
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识", required = true),
        @Parameter(name = "pendantId", type = Type.Integer, description = "挂件标识", required = false),
        @Parameter(name = "type", type = Type.String, description = "类型（1-设置 2-取消）", required = false),
})
@Component
public class UpdatePendantUrl implements Action {

    @Autowired
    private UserService userService;

    private static final Logger logger = LoggerFactory.getLogger(ListFans.class);

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        Long pendantId = request.getLong("pendantId");
        String type = request.getString("type");

        try {
            if (StringUtil.isNotEmpty(type) && "2".equals(type)){
                userService.resetPendantUrl(userId);
            }else {
                if (null == pendantId){
                    return Response.fail("您未选择头像挂件");
                }
                userService.updatePendantUrl(userId, pendantId);
            }

            return Response.success();
        } catch (Exception e) {
            if (e instanceof BizException) {
                BizException se = (BizException) e;
                return Response.fail(se.getErrMsg(), se.getErrCode());
            }
            logger.info("", e);
            return Response.fail("操作异常");
        }
    }

}
